package com.zrsx.managemodel.task;

import com.zrsx.managemodel.service.CreditManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Component
@Slf4j
public class CreditTask {

    @Autowired
    private CreditManageService creditManageService;

    /**
     * 每天凌晨0点执行，更新昨天发生变化的总信用分维度的描述字段
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void updateTotalCreditScoreDesc() {
        log.info("开始更新总信用分维度描述任务，时间：{}", LocalDateTime.now());

        LocalDate yesterday = LocalDate.now().minusDays(1);
        LocalDateTime start = yesterday.atStartOfDay();              // 昨天00:00:00
        LocalDateTime end = yesterday.atTime(LocalTime.MAX);         // 昨天23:59:59

        try {
            int updatedCount = creditManageService.updateTotalCreditDescBetween(start, end);
            log.info("总信用分维度描述更新完成，更新条数：{}", updatedCount);
        } catch (Exception e) {
            log.error("更新总信用分维度描述任务失败", e);
        }
    }
}
